package cn.itcast.service.impl;

import cn.itcast.dao.AreaDao;
import cn.itcast.domain.Area;
import cn.itcast.service.AreaService;
import cn.itcast.utils.PinYin4jUtils;

import com.sun.deploy.net.HttpResponse;
import com.sun.org.apache.bcel.internal.generic.NEW;
import net.sourceforge.pinyin4j.PinyinHelper;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.List;
import java.util.UUID;

/**
 * @Author： ~Mr·Weng~
 * @CreateTime： 2019年-01月-23日  14:50
 * Look at the wine in the glass toast the rich first.
 */
@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    private AreaDao areaDao;

    @Override
    public void saveAll(List<Area> areaList) {

        String str = "";
        String citycode = "";
        for (Area area : areaList) {

            //省份的首字母
            StringBuilder sbP = new StringBuilder(area.getProvince());
            String strP = sbP.deleteCharAt(area.getProvince().length()-1).toString();
            String[] strPArry = PinYin4jUtils.getHeadByString(strP);
            str += PinYin4jUtils.stringArrayToString(strPArry);

            //城市的首字母
            StringBuilder sbC = new StringBuilder(area.getCity());
            String strC = sbC.deleteCharAt(area.getCity().length() - 1).toString();
            //城市编码
            citycode += PinYin4jUtils.hanziToPinyin(strC, "");
            //首字母
            String[] strCArry = PinYin4jUtils.getHeadByString(strC);
            str += PinYin4jUtils.stringArrayToString(strCArry);


            //区首字母
            StringBuilder sbD = new StringBuilder(area.getDistrict());
            String strD = sbD.deleteCharAt(area.getDistrict().length()-1).toString();
            String[] strDArry = PinYin4jUtils.getHeadByString(strD);
            str += PinYin4jUtils.stringArrayToString(strDArry);

            area.setCitycode(citycode);
            area.setShortcode(str);

            str = "";
            citycode = "";

            areaDao.save(area);
        }

    }

    /**
     * 分页查询
     * @param area
     * @param page
     * @param rows
     * @return
     */
    @Override
    public Page<Area> findAll(Area area, int page, int rows) {

        Pageable pageable = PageRequest.of(page - 1, rows);

        ExampleMatcher withMatcher = ExampleMatcher.matchingAll()
                .withMatcher("province", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("city", ExampleMatcher.GenericPropertyMatchers.contains())
                .withMatcher("district", ExampleMatcher.GenericPropertyMatchers.contains());

        Example<Area> pageableExample = Example.of(area, withMatcher);
        return areaDao.findAll(pageableExample,pageable);
    }

    @Override
    public void save(Area area) {
        String shouzimu = "";

        //省份首字母
        StringBuilder sbP = new StringBuilder(area.getProvince());
        String pStr = sbP.deleteCharAt(area.getProvince().length() - 1).toString();
        String[] pStrArray = PinYin4jUtils.getHeadByString(pStr);
        shouzimu += PinYin4jUtils.stringArrayToString(pStrArray);

        //城市首字母
        StringBuilder sbC = new StringBuilder(area.getCity());
        String cStr = sbC.deleteCharAt(area.getCity().length() - 1).toString();
        String[] cStrArray = PinYin4jUtils.getHeadByString(cStr);
        shouzimu += PinYin4jUtils.stringArrayToString(cStrArray);
        String citycode = PinYin4jUtils.hanziToPinyin(cStr,"");


        //区首字母
        StringBuilder sbD = new StringBuilder(area.getDistrict());
        String dStr = sbD.deleteCharAt(area.getDistrict().length() - 1).toString();
        String[] dStrArray = PinYin4jUtils.getHeadByString(dStr);
        shouzimu += PinYin4jUtils.stringArrayToString(dStrArray);

        area.setCitycode(citycode);
        area.setShortcode(shouzimu);

        shouzimu = "";

        areaDao.save(area);
    }

    @Override
    public List<Area> area_findAll() {
        Sort sort = new Sort(new Sort.Order(Sort.Direction.DESC,"id"));
        List<Area> areaList = areaDao.findAll(sort);
        return areaList;
    }

    @Override
    public void findAllExport() throws Exception {

        HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
        HSSFSheet sheet = hssfWorkbook.createSheet("第一页");

        List<Area> areaList = areaDao.findAll();
        HSSFRow row = sheet.createRow(0);//标题行
        row.createCell(0).setCellValue("区域编号");
        row.createCell(1).setCellValue("省份");
        row.createCell(2).setCellValue("城市");
        row.createCell(3).setCellValue("城市编码");
        row.createCell(4).setCellValue("区域");
        row.createCell(5).setCellValue("邮编");
        row.createCell(6).setCellValue("简码");

            for (Area area : areaList) {
                HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum()+ 1);
                    dataRow.createCell(0).setCellValue(area.getId());
                    dataRow.createCell(1).setCellValue(area.getProvince());
                    dataRow.createCell(2).setCellValue(area.getCity());
                    dataRow.createCell(3).setCellValue(area.getCitycode());
                    dataRow.createCell(4).setCellValue(area.getDistrict());
                    dataRow.createCell(5).setCellValue(area.getPostcode());
                    dataRow.createCell(6).setCellValue(area.getShortcode());

            }

        String filename = URLEncoder.encode("bos.xls", "UTF-8");
            FileOutputStream fos = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\"+filename);
            hssfWorkbook.write(fos);






        }




}
